package com.eascs.cloudFile.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.app.context.ConfigContext;
import com.eascs.app.dao.PageResult;
import com.eascs.cloudFile.entity.CloudDatalist;
import com.eascs.cloudFile.service.AdminFileService;
import com.eascs.cloudFile.service.CrmFileService;
import com.eascs.cloudFile.service.WebCloudFileService;
import com.eascs.cloudFile.vo.CloudFileVo;
import com.eascs.cloudFile.vo.SaveFileVo;
import com.eascs.cloudFile.vo.WebCloudFileVo;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.AjaxConfirmButton;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;

@RestController
@RequestMapping("/cloudFilesRest")
public class CloudFilesRestController{

	@Autowired
	CrmFileService crmFileService;

	@Autowired
	AdminFileService adminFileService;
	
	@Autowired
	WebCloudFileService webCloudFileService;

	@RequestMapping("/toSelectCloudFiles")
	public BaseRestResult<String> viewUpdate(CloudFileVo cloudFileVo) {
		PageResult<Object[]> pageResult = crmFileService.queryCloudFileList(cloudFileVo);
		String titles[] = { "附件类型", "拍摄时间", "拍摄地点" };
		Button[] titleButtons = new Button[] { ButtonHelper.createOpenButton(null, ConfigContext.getStringValue("attachment.cloid.view.url") + "{1}") };
		AjaxConfirmButton[] button = new AjaxConfirmButton[] { new AjaxConfirmButton("选择此云文档上传 ", "/cloudFilesRest/cloudToFds?loanapplyNo=" + cloudFileVo.getLoanapplyNo() + "&dataListType=" + cloudFileVo.getDatalistCode() + "&id=") };
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles);
		paginationInfo.setMoreButtons(button);
		paginationInfo.setTitleButtons(titleButtons);
		paginationInfo.setBuildPagingNavigation(false);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setBuildRowNumber(true);
		ViewContext.put("pageResult", paginationInfo);
		return HtmlRestResult.popHtmlFragment("/cloudFile/toSelectCloudList");
	}

	@RequestMapping("/cloudToFds")
	public BaseRestResult<String> cloudToFds(CloudFileVo cloudFileVo, String id) {
		SaveFileVo saveFileVo = new SaveFileVo().setId(id).setDataListType(cloudFileVo.getDatalistCode()).setLoanapplyNo(cloudFileVo.getLoanapplyNo());
		adminFileService.saveFileToCloud(saveFileVo);
		return BaseRestResult.info("附件上传成功").andRefresh();
	}
	
	@RequestMapping("/testCloudFiles")
	public List<CloudDatalist> testCloudFiles(WebCloudFileVo webCloudFileVo) {
		return webCloudFileService.queryCloudDatas(webCloudFileVo);
	}
}
